#include "CheckBox.h"

#include "../_Luly.h"
#include "_UI.h"
CheckBox::CheckBox(std::string uiObjectId, int x, int y)
    : UIObject(uiObjectId, x, y) {
  default_check_image = _UI::getInstance().checkbox_check_default_image;
  default_box_image = _UI::getInstance().checkbox_box_default_image;
  checked_check_image = _UI::getInstance().checkbox_check_checked_image;
}

void CheckBox::init() {}
void CheckBox::event() {
  SDL_Event current_event = _Luly::getInstance().current_event;
  int x, y;
  SDL_GetMouseState(&x, &y);
  x = int(x / _Luly::getInstance().scale);
  y = int(y / _Luly::getInstance().scale);
  switch (current_event.type) {
    case SDL_MOUSEMOTION:
      break;
    case SDL_MOUSEBUTTONDOWN:
      if ((x > CheckBox::x && x < CheckBox::x + width) &&
          (y > CheckBox::y && y < CheckBox::y + height)) {
        isChecked = !isChecked;
        onClicked_callback(isChecked);
      }
      break;
    case SDL_MOUSEBUTTONUP:
      break;
  }
}

void CheckBox::draw() {
  // if (isChecked == false) {
  //   current_check_image = default_check_image;
  //   current_box_image = default_box_image;
  // } else {
  //   current_check_image = checked_check_image;
  // }
  // current_box_image.draw(x, y);
  // current_check_image.draw(x, y);
  // width = current_box_image.getWidth();
  // height = current_box_image.getHeight();
  drawArea(Rect{x, y, width, height});
}

void CheckBox::drawArea(const Rect& area) {
  if (isChecked == false) {
    current_check_image = default_check_image;
    current_box_image = default_box_image;
  } else {
    current_check_image = checked_check_image;
  }
  current_box_image.drawArea(area, Rect{0, 0, width, height},
                             Rect{x, y, width, height}, angle);
  current_check_image.drawArea(area, Rect{0, 0, width, height},
                               Rect{x, y, width, height}, angle);
  width = current_box_image.getWidth();
  height = current_box_image.getHeight();
}

void CheckBox::onClicked(std::function<void(bool)> onClicked_callback) {
  CheckBox::onClicked_callback = onClicked_callback;
}